package main

import (
	"context"
	"fmt"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/sdk/trace"
	"io"
	"net/http"
)

func init() {
	tp := trace.NewTracerProvider()
	otel.SetTracerProvider(tp)
}
func main() {
	for {
		tracer := otel.GetTracerProvider().Tracer("")
		ctx, span := tracer.Start(context.Background(), "Client/User defined span")
		otel.GetTextMapPropagator()
		req, err := http.NewRequestWithContext(ctx, "GET", "http://127.0.0.1:8080/rolldice", nil)
		if err != nil {
			fmt.Println(err.Error())
			continue
		}
		client := &http.Client{}
		resp, err := client.Do(req)
		if err != nil {
			fmt.Println(err.Error())
			continue
		}
		defer resp.Body.Close()
		b, err := io.ReadAll(resp.Body)
		if err != nil {
			fmt.Println(err.Error())
			continue
		}
		fmt.Println(string(b))
		span.SetAttributes(attribute.String("client", "client-with-ot"))
		span.SetAttributes(attribute.Bool("user.defined", true))
		span.End()
	}
}
